package com.example.fish.controller;

import com.example.fish.pojo.Blog;
import com.example.fish.pojo.User;
import com.example.fish.service.BlogService;
import com.example.fish.utils.JsonResult;
import com.example.fish.vo.BlogList;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    /***
     * 查询所有的博客列表
     * @return List<Blog>
     */
    @GetMapping("/all")
    public JsonResult<List<Blog>> getAll(){
        List<Blog> allBlog = blogService.findAllBlog();
        return new JsonResult<>(200,"success",allBlog);
    }

    /***
     *  添加博客
     * @param blog blog对象
     * @param file fileList
     * @param session session
     * @return void
     */
    @PostMapping("/addBlog")
    public JsonResult<Void> addBlog(Blog blog,List<MultipartFile> file,HttpSession session){
        User user = (User) session.getAttribute("user");
        blog.setUserid(user.getUserid());
        blogService.insertBlog(blog,file);
        return new JsonResult<>(200,"success",null);
    }

    /***
     * 删除blog
     * @param blogid id
     * @return void
     */
    @PostMapping("/deleteBlog")
    public JsonResult<Void> deleteBlog(Integer blogid){
//        System.out.println("deleteBlog:"+blogid);
        blogService.deleteBlog(blogid);
        return new JsonResult<>(200,"success",null);
    }

    /***
     * 获取当前登录用户 的博客
     */
    @GetMapping("/getOwnBlog")
    public JsonResult<List<Blog>> getOwnBlog(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Blog> blogs = blogService.findBlogByUserId(user.getUserid());
        return new JsonResult<>(200,"success",blogs);
    }

    /***
     * 根据用户id分页查询
     * @param pageNum 页码数
     * @return 分页信息
     */
    @PostMapping("/page")
    public JsonResult<PageInfo<Blog>> getBlogPage(Integer pageNum,HttpSession session){
        Integer pageSize = 5;
        User user = (User) session.getAttribute("user");
        PageInfo<Blog> page = blogService.findBlogByUserIdPage(pageNum, pageSize, user.getUserid());
        return new JsonResult<>(200,"success",page);
    }

    /***
     * 主页的分页查询
     * @param pageNum 当前页数
     * @return PageInfo
     */
    @PostMapping("/homePage")
    public JsonResult<PageInfo<Blog>> getAllBlogByPage(Integer pageNum){
        Integer pageSize = 5;
        PageInfo<Blog> page = blogService.getAllBlogByPage(pageNum, pageSize);
        return new JsonResult<>(200,"success",page);
    }
    @PostMapping("/agree")
    public JsonResult<Void> agreeBlog(Integer blogid){
        blogService.agreeBlog(blogid);
        return new JsonResult<>(200,"点赞成功",null);
    }
    @PostMapping("/deagree")
    public JsonResult<Void> deagreeBlog(Integer blogid){
        blogService.agreeDeBlog(blogid);
        return new JsonResult<>(200,"取消点赞成功",null);
    }
    @PostMapping("/disagree")
    public JsonResult<Void> disagreeBlog(Integer blogid){
        blogService.disagreeBlog(blogid);
        return new JsonResult<>(200,"踩一脚成功",null);
    }
    @PostMapping("/dedisagree")
    public JsonResult<Void> dedisagreeBlog(Integer blogid){
        blogService.disagreeDeBlog(blogid);
        return new JsonResult<>(200,"取消踩一脚成功",null);
    }

    @PostMapping("/blogById")
    public JsonResult<Blog> getBlogById(Integer blogid){
        Blog blog = blogService.findBlogById(blogid);
        return new JsonResult<>(200,"success",blog);
    }

    @PostMapping("/home")
    public JsonResult<PageInfo<BlogList>> homeBlogs(Integer pageNum){
        PageInfo<BlogList> blogListPageInfo = blogService.homePage(pageNum);
        return new JsonResult<>(200,"success",blogListPageInfo);
    }

}
